package cn.stylefeng.guns.modular.tool.service.impl;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.entity.BasicCustomer;
import cn.stylefeng.guns.modular.basic.entity.BasicWarehouse;
import cn.stylefeng.guns.modular.basic.model.params.BasicGoodsParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicGoodsResult;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicGoodsService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.order.entity.OutOrder;
import cn.stylefeng.guns.modular.order.model.params.OutOrderDetailedParam;
import cn.stylefeng.guns.modular.order.model.params.OutOrderParam;
import cn.stylefeng.guns.modular.order.service.OutOrderDetailedService;
import cn.stylefeng.guns.modular.order.service.OutOrderService;
import cn.stylefeng.guns.modular.storage.entity.StorageOutDetailed;
import cn.stylefeng.guns.modular.storage.model.params.StorageOutParam;
import cn.stylefeng.guns.modular.storage.service.StorageOutDetailedService;
import cn.stylefeng.guns.modular.storage.service.StorageOutService;
import cn.stylefeng.guns.modular.storage.service.StorageStockDetailedService;
import cn.stylefeng.guns.modular.storage.service.StorageStockService;
import cn.stylefeng.guns.modular.tool.entity.ToolExpressSumTask;
import cn.stylefeng.guns.modular.tool.entity.ToolWave;
import cn.stylefeng.guns.modular.tool.mapper.ToolExpressSumTaskMapper;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressExcelParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressSumTaskParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolInstallParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolStorageStockParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolTaskMoneyParam;
import cn.stylefeng.guns.modular.tool.model.result.ToolExpressExcelResult;
import cn.stylefeng.guns.modular.tool.model.result.ToolExpressSumTaskResult;
import cn.stylefeng.guns.modular.tool.model.result.ToolInstallResult;
import cn.stylefeng.guns.modular.tool.model.result.ToolStorageStockResult;
import cn.stylefeng.guns.modular.tool.service.ToolExpressExcelService;
import  cn.stylefeng.guns.modular.tool.service.ToolExpressSumTaskService;
import cn.stylefeng.guns.modular.tool.service.ToolInstallService;
import cn.stylefeng.guns.modular.tool.service.ToolStorageStockService;
import cn.stylefeng.guns.modular.tool.service.ToolTaskMoneyService;
import cn.stylefeng.guns.modular.tool.service.ToolWaveService;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * 作业统计管理 服务实现类
 */
@Service
public class ToolExpressSumTaskServiceImpl extends ServiceImpl<ToolExpressSumTaskMapper, ToolExpressSumTask> implements ToolExpressSumTaskService {

    @Override
    public void add(ToolExpressSumTaskParam param){
        ToolExpressSumTask entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ToolExpressSumTaskParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ToolExpressSumTaskParam param){
        ToolExpressSumTask oldEntity = getOldEntity(param);
        ToolExpressSumTask newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ToolExpressSumTaskResult findBySpec(ToolExpressSumTaskParam param){
        return null;
    }

    @Override
    public List<ToolExpressSumTaskResult> findListBySpec(ToolExpressSumTaskParam param){
        return this.baseMapper.findListBySpec(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(ToolExpressSumTaskParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findPageBySpectj(ToolExpressSumTaskParam param,String condition,Long operateId){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageListtj(pageContext, param,condition, operateId);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findPageBySpectjnum(ToolExpressSumTaskParam param,String condition,Long operateId){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageListtjnum(pageContext, param,condition, operateId);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findPageBySpecd(ToolExpressSumTaskParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageListd(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findPageSumBySpec(ToolExpressSumTaskParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findPageSumBySpec(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ToolExpressSumTaskParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ToolExpressSumTask getOldEntity(ToolExpressSumTaskParam param) {
        return this.getById(getKey(param));
    }

    private ToolExpressSumTask getEntity(ToolExpressSumTaskParam param) {
        ToolExpressSumTask entity = new ToolExpressSumTask();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

	@Override
	public void deleteByExpressSumId(ToolExpressSumTaskParam expressSumTaskParam) {
		this.baseMapper.deleteByExpressSumId(expressSumTaskParam);
	}

	@Override
	public void deleteBySpec(ToolExpressSumTaskParam expressSumTaskParam) {
		this.baseMapper.deleteBySpec(expressSumTaskParam);
		
	}

	@Override
	public boolean saveBatchList(List<ToolExpressSumTaskParam> savelist) {
		return this.baseMapper.saveBatchList(savelist);
	}

    @Resource
    private ToolExpressExcelService toolExpressExcelService;

    @Resource
    private StorageStockService storageStockService;

    @Resource
    private BasicGoodsService basicGoodsService;
    
    @Resource
    private OutOrderService outOrderService;

	@Resource
    private DeptService deptService;
	
	/**
	 * 获取所属项目单号前缀
	 * @param user
	 * @return
	 */
	public String getOperateIdOrderPrefix(User user) {
		if(user.getOperateId() != null) {
			Dept dept = new Dept();
			dept.setOperateId(user.getOperateId());
			Dept resdept = deptService.findBySpec(dept);
			if(resdept != null) {
				if(ToolUtil.isNotEmpty(resdept.getOrderPrefix())) {
					return resdept.getOrderPrefix();
				} else {
					return user.getAccount().substring(0, 3);
				}
			} else {
				return null;
			}
		} else {
			return null;
		}
	}
	
	@Resource
    private StorageOutService storageOutService;

	@Resource
    private BasicWarehouseService basicWarehouseService;

	@Resource
    private BasicCustomerService basicCustomerService;
	@Resource
    private ToolStorageStockService toolStorageStockService;

    /**
     * 根据作业信息
     * 更新WMS系统库存
     */
	@Override
	@Transactional(rollbackFor = Exception.class,timeout = 600)
	public ResponseData updateStock(ToolExpressExcelParam toolExpressExcelParam,Long outStorehouseId, User user) {
    	List<ToolExpressExcelResult> list = toolExpressExcelService.findSumListBySpec(toolExpressExcelParam);
    	if(list.size()==0) {
    		return ResponseData.error("未获取到导入快递明细");
    	}
    	BasicCustomer basicCustomer = basicCustomerService.getById(toolExpressExcelParam.getCustomerId());
    	BasicWarehouse basicWarehouse = basicWarehouseService.getById(outStorehouseId);
    	//添加出库订单
    	OutOrderParam outOrderParam = new OutOrderParam();
    	outOrderParam.setOutOrderStatus("已出库");
        OutOrder outOrder = this.outOrderService.findByTheLastOne();
		String trayCode = getOperateIdOrderPrefix(user);
        if (outOrder != null){
            outOrderParam.setOutOrderNumber(trayCode+"-"+"CK-"+DateUtil.getDateBch()+"-"+(outOrder.getId()+1));
            outOrderParam.setCustomerNumber(trayCode+"-"+"CK-"+DateUtil.getDateBch()+"-"+(outOrder.getId()+1));
        } else {
            outOrderParam.setOutOrderNumber(trayCode+"-"+"CK-"+DateUtil.getDateBch()+"-"+"1");
            outOrderParam.setCustomerNumber(trayCode+"-"+"CK-"+DateUtil.getDateBch()+"-"+"1");
        }
        outOrderParam.setOutCustomerId(toolExpressExcelParam.getCustomerId());
        outOrderParam.setOutCustomerName(basicCustomer.getCustomerName());
        outOrderParam.setOutTime(new Date());
        outOrderParam.setOutWay("快递自提");
        outOrderParam.setOutStorehouseId(outStorehouseId);
        outOrderParam.setStorehouseName(basicWarehouse.getWarehouseName());
        outOrderParam.setTransportWay("快递");
        outOrderParam.setSetMentiontime(DateUtil.getDate());
        outOrderParam.setDeptId(user.getDeptId());
        outOrderParam.setDeliverId(user.getDeliverId());
        outOrderParam.setCreateUserName(user.getName());
        outOrderService.add(outOrderParam);
        
        //添加出库单
        StorageOutParam storageOutParam = new StorageOutParam();
        storageOutParam.setStorageOutNumber(outOrderParam.getCustomerNumber());
        storageOutParam.setOutOrderNumber(outOrderParam.getOutOrderNumber());//出库订单编号
        storageOutParam.setOutStorehouseId(outOrderParam.getOutStorehouseId());//库房id
        storageOutParam.setStorehouseName(outOrderParam.getStorehouseName());//库房名称
        storageOutParam.setDeliverId(outOrderParam.getDeliverId());//交付id
        storageOutParam.setDeptId(outOrderParam.getDeptId());
        storageOutParam.setOutOrderStatus("已完成");
        storageOutParam.setSetMentiontime(outOrderParam.getSetMentiontime());//发货时间 
        storageOutService.add(storageOutParam);

		List<OutOrderDetailedParam> detailedList = new ArrayList<OutOrderDetailedParam>();//批量保存的 出库订单明细list
        List<StorageOutDetailed> solist2 = new ArrayList<StorageOutDetailed>();//要保存的 拣货单明细list
    	String message = "";
    	boolean errorFalg = true;
        for (ToolExpressExcelResult er : list) {
        	if(er.getGoodsNumber().equals("1759947952444423")||er.getGoodsNumber().equals("1759325202211891")||er.getGoodsNumber().equals("4967199140507")||er.getGoodsNumber().equals("1759325202211890")
        			||er.getGoodsNumber().equals("1759947952444420")||er.getGoodsNumber().equals("687374017518")||er.getGoodsNumber().equals("693717971892")
        			||er.getGoodsNumber().equals("caixiang")
        			||er.getGoodsNumber().equals("qipaodai-05")
        			||er.getGoodsNumber().equals("goodcard5")
        			||er.getGoodsNumber().equals("TM-R0")||er.getGoodsNumber().equals("TM-NK")
        			||er.getGoodsNumber().equals("TM-RO")
        			||er.getGoodsNumber().equals("zhixiang")||er.getGoodsNumber().equals("zhixiang-A1")||er.getGoodsNumber().equals("zhixiang-A3")
        			||er.getGoodsNumber().equals("会员福利卡")||er.getGoodsNumber().equals("瑜大公子卡片")||er.getGoodsNumber().equals("纸箱包装")) {
        		continue;
        	}
            BasicGoodsParam basicGoodsParam2 = new BasicGoodsParam();
            basicGoodsParam2.setGoodsNumber(er.getGoodsNumber());
            basicGoodsParam2.setGoodsCustomerId(Integer.parseInt(er.getCustomerId().toString()));
    		BasicGoodsResult basicGoodsResult2 = basicGoodsService.findBySpec(basicGoodsParam2,null);
    		if(basicGoodsResult2 == null) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
        		return ResponseData.error("未查询到"+er.getGoodsNumber()+"基础信息");
    		}
			BigDecimal detailedPutnum = new BigDecimal(er.getNum());//出库数量
			BigDecimal detailedPutnumx = new BigDecimal(er.getNum());//出库数量
			BigDecimal w = new BigDecimal(basicGoodsResult2.getGoodsWeight());//重量
			BigDecimal v = new BigDecimal(basicGoodsResult2.getGoodsVolume());//体积
			BigDecimal u = new BigDecimal(basicGoodsResult2.getGoodsCharging());//单价compareTo
			BigDecimal as = new BigDecimal(0);
            
        	OutOrderDetailedParam orderDetailedParam = new OutOrderDetailedParam();
        	orderDetailedParam.setDetailedOutNumber(outOrderParam.getOutOrderNumber());
        	orderDetailedParam.setDetailedGoodsId(Long.parseLong(basicGoodsResult2.getId().toString()));
        	orderDetailedParam.setDetailedGoodscode(basicGoodsResult2.getGoodsNumber());
        	orderDetailedParam.setDetailedGoodsname(basicGoodsResult2.getGoodsName());
        	orderDetailedParam.setDetailedPutbalenum(er.getNum());
        	orderDetailedParam.setDetailedNumber(er.getNum());
        	orderDetailedParam.setDetailedWeight(basicGoodsResult2.getGoodsWeight());
        	orderDetailedParam.setDetailedOutWeight(w.multiply(detailedPutnum).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
        	orderDetailedParam.setDetailedVolume(basicGoodsResult2.getGoodsVolume());
        	orderDetailedParam.setDetailedOutVolume(v.multiply(detailedPutnum).setScale(4, BigDecimal.ROUND_HALF_DOWN).toString());
        	orderDetailedParam.setDetailedPrice(basicGoodsResult2.getGoodsCharging());
        	orderDetailedParam.setDetailedMoney(u.multiply(detailedPutnum).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
        	orderDetailedParam.setDetailedRebubbles(basicGoodsResult2.getGoodsHeavybubble());
        	orderDetailedParam.setDetailedPacking(basicGoodsResult2.getGoodsPacking());
        	orderDetailedParam.setDetailedLargeclass(basicGoodsResult2.getGoodsBigtype());
        	orderDetailedParam.setDetailedGoodstype(basicGoodsResult2.getGoodsSmalltype());
        	orderDetailedParam.setDetailedPutcondition(basicGoodsResult2.getGoodsCondition());
        	orderDetailedParam.setGoodsUnit(basicGoodsResult2.getGoodsUnit());
        	orderDetailedParam.setGoodsSpecs(basicGoodsResult2.getGoodsSpecs());
        	detailedList.add(orderDetailedParam);
        	
        	ToolStorageStockParam storageStockParam = new ToolStorageStockParam();
        	storageStockParam.setGoodsId(Long.parseLong(basicGoodsResult2.getId().toString()));
        	storageStockParam.setPositionNumber(er.getArrangeNumber());
        	List<ToolStorageStockResult> slist = toolStorageStockService.findListBySpec(storageStockParam);
        	if(slist.size()==0) {
                errorFalg = false;
                message += er.getArrangeNumber()+"小组"+er.getGoodsNumber()+"需求"+detailedPutnumx.toString()+"缺少"+detailedPutnum.toString()+"；";
                continue;
        	}
        	for (ToolStorageStockResult sr : slist) {
    			BigDecimal num = new BigDecimal(sr.getNum());//库存数
        		if(num.compareTo(detailedPutnum) == 1) {
        			num = num.subtract(detailedPutnum).setScale(2, BigDecimal.ROUND_HALF_DOWN);
        			ToolStorageStockParam storageStockUpdate = new ToolStorageStockParam();
        			ToolUtil.copyProperties(sr, storageStockUpdate);
        			storageStockUpdate.setNum(num.toString());
        			storageStockUpdate.setStockWeight(w.multiply(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
        			storageStockUpdate.setStockVolume(v.multiply(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
        			storageStockUpdate.setStockMoney(u.multiply(num).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
        			toolStorageStockService.update(storageStockUpdate);
        			
                    StorageOutDetailed storage = prepareViewData(orderDetailedParam);
                    storage.setStorageOutNumber(storageOutParam.getStorageOutNumber());//出库单
                    storage.setStoragePostionNumber(sr.getPositionNumber());//库位编码
                    storage.setRealNum(String.valueOf(detailedPutnum));
                    storage.setStorageNumber(String.valueOf(detailedPutnum));//出库数量为拣货单数量
                    storage.setStoragePutbalenum(String.valueOf(detailedPutnum));//出库数量为拣货单大包数
                    storage.setStorageWeight(String.valueOf(detailedPutnum.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总重量
                    storage.setStorageVolume(String.valueOf(detailedPutnum.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总体积
                    storage.setStorageMoney(String.valueOf(u.multiply(detailedPutnum).setScale(3, BigDecimal.ROUND_HALF_DOWN)));
                    storage.setStockId(Long.parseLong(sr.getId().toString()));//库存id
                    solist2.add(storage);
        			detailedPutnum = as;
        			break;
        		}
        		if(num.compareTo(detailedPutnum) == 0) {
        			ToolStorageStockParam storageStockUpdate = new ToolStorageStockParam();
        			ToolUtil.copyProperties(sr, storageStockUpdate);
        			toolStorageStockService.delete(storageStockUpdate);
        			
                    StorageOutDetailed storage = prepareViewData(orderDetailedParam);
                    storage.setStorageOutNumber(storageOutParam.getStorageOutNumber());//出库单
                    storage.setStoragePostionNumber(sr.getPositionNumber());//库位编码
                    storage.setRealNum(String.valueOf(detailedPutnum));
                    storage.setStorageNumber(String.valueOf(detailedPutnum));//出库数量为拣货单数量
                    storage.setStoragePutbalenum(String.valueOf(detailedPutnum));//出库数量为拣货单大包数
                    storage.setStorageWeight(String.valueOf(detailedPutnum.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总重量
                    storage.setStorageVolume(String.valueOf(detailedPutnum.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总体积
                    storage.setStorageMoney(String.valueOf(u.multiply(detailedPutnum).setScale(2, BigDecimal.ROUND_HALF_DOWN)));
                    storage.setStockId(Long.parseLong(sr.getId().toString()));//库存id
                    solist2.add(storage);
        			detailedPutnum = as;
        			break;
        		}
        		if(num.compareTo(detailedPutnum) == -1) {
        			ToolStorageStockParam storageStockUpdate = new ToolStorageStockParam();
        			ToolUtil.copyProperties(sr, storageStockUpdate);
        			toolStorageStockService.delete(storageStockUpdate);
        			detailedPutnum = detailedPutnum.subtract(num).setScale(2, BigDecimal.ROUND_HALF_DOWN);
        			
                    StorageOutDetailed storage = prepareViewData(orderDetailedParam);
                    storage.setStorageOutNumber(storageOutParam.getStorageOutNumber());//出库单
                    storage.setStoragePostionNumber(sr.getPositionNumber());//库位编码
                    storage.setRealNum(String.valueOf(num));
                    storage.setStorageNumber(String.valueOf(num));//出库数量为拣货单数量
                    storage.setStoragePutbalenum(String.valueOf(num));//出库数量为拣货单大包数
                    storage.setStorageWeight(String.valueOf(num.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总重量
                    storage.setStorageVolume(String.valueOf(num.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总体积
                    storage.setStorageMoney(String.valueOf(u.multiply(num).setScale(2, BigDecimal.ROUND_HALF_DOWN)));
                    storage.setStockId(Long.parseLong(sr.getId().toString()));//库存id
                    solist2.add(storage);
        		}
			}
        	if(detailedPutnum.compareTo(as) == 1) {
                errorFalg = false;
                message += er.getArrangeNumber()+"小组"+er.getGoodsNumber()+"需求"+detailedPutnumx.toString()+"缺少"+detailedPutnum.toString()+"；";
        	}
		}
        if (!errorFalg) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
        	return ResponseData.error(message);
		}
        if(detailedList.size()>0) {
        	outOrderDetailedService.saveBatchList(detailedList);
        	storageOutDetailedService.saveBatchList(solist2);
        }
		return ResponseData.success();
	}

    /**
     * 拣货单明细
     * @param orderDetailedResult
     * @return
     */
    private StorageOutDetailed prepareViewData(OutOrderDetailedParam orderDetailedResult){
        StorageOutDetailed storage = new StorageOutDetailed();
        //商品编码
        storage.setStorageGoodscode(orderDetailedResult.getDetailedGoodscode());
        //商品id
        storage.setStorageGoodsId(orderDetailedResult.getDetailedGoodsId());
        //商品名称
        storage.setStorageGoodsname(orderDetailedResult.getDetailedGoodsname());
        //重量
        storage.setStorageWeight(orderDetailedResult.getDetailedWeight());
        //体积
        storage.setStorageVolume(orderDetailedResult.getDetailedVolume());
        //单价
        storage.setStoragePrice(orderDetailedResult.getDetailedPrice());
        //金额
        storage.setStorageMoney(orderDetailedResult.getDetailedMoney());
        //重泡货
        storage.setStorageRebubbles(orderDetailedResult.getDetailedRebubbles());
        //商品包装
        storage.setStoragePacking(orderDetailedResult.getDetailedPacking());
        //商品大类
        storage.setStorageLargeclass(orderDetailedResult.getDetailedLargeclass());
        //商品类别
        storage.setStorageGoodstype(orderDetailedResult.getDetailedGoodstype());
        //保管条件
        storage.setStoragePutcondition(orderDetailedResult.getDetailedPutcondition());
        //单位
        storage.setGoodsUnit(orderDetailedResult.getGoodsUnit());
        //规格
        storage.setGoodsSpecs(orderDetailedResult.getGoodsSpecs());
        //出库明细状态
        storage.setStorageOutStatus("已拣货");
        return storage;
    }

    @Resource
    private StorageStockDetailedService storageStockDetailedService;

    @Resource
    private StorageOutDetailedService storageOutDetailedService;

    @Resource
    private OutOrderDetailedService outOrderDetailedService;
    
    @Autowired
    private ToolWaveService toolWaveService;
    
    @Autowired
    private ToolInstallService toolInstallService;

    @Autowired
    private ToolTaskMoneyService toolTaskMoneyService;

	@Override
	@Transactional(rollbackFor = Exception.class,timeout = 300)
	public ResponseData batchassign(ToolExpressSumTaskParam toolExpressSumTaskParam, User user) {
    	ToolInstallParam installParam = new ToolInstallParam();
    	installParam.setPriceType("订单");
    	installParam.setDeptId(user.getDeptId());
    	List<ToolInstallResult> listi = toolInstallService.findListBySpec(installParam,user.getOperateId());//查询计件单价设置
    	if(listi.size()==0) {
    		return ResponseData.error("未查询到订单计件工资设置");
    	}
    	
    	ToolInstallParam installParam2 = new ToolInstallParam();
    	installParam2.setPriceType("预包");
    	installParam2.setDeptId(user.getDeptId());
    	List<ToolInstallResult> listiy = toolInstallService.findListBySpec(installParam2,user.getOperateId());//查询计件单价设置
    	if(listiy.size()==0) {
    		return ResponseData.error("未查询到预包计件工资设置");
    	}
    	
    	List<ToolInstallResult> listijf = new ArrayList<ToolInstallResult>();

    	ToolTaskMoneyParam moneyParam = new ToolTaskMoneyParam();
    	moneyParam.setWaveId(toolExpressSumTaskParam.getWaveId());
    	toolTaskMoneyService.deleteBySpec(moneyParam);//删除工资明细

    	ToolWave toolWave = toolWaveService.getById(toolExpressSumTaskParam.getWaveId());//波次单
    	List<ToolExpressSumTaskResult> list = this.findListBySpec(toolExpressSumTaskParam);//作业明细
    	List<ToolTaskMoneyParam> listsave = new ArrayList<ToolTaskMoneyParam>();
    	for (ToolExpressSumTaskResult r : list) {
			BigDecimal num = new BigDecimal(r.getNum());//组合数量
			BigDecimal v1 = new BigDecimal(r.getSumNum());//单量
    		boolean falg = false;
    		if(ToolUtil.isNotEmpty(r.getV3())) {
	    		if(r.getV3().equals("预包")) {
	    			listijf = listiy;
	    		} else {
	    			listijf = listi;
	    		}
    		} else {
    			listijf = listi;
    		}
			for (ToolInstallResult rr : listijf) {
				if(r.getCustomerId().equals(rr.getCustomerId())) {
					if(rr.getGoodsMode().equals("区间")) {
						BigDecimal gNum = new BigDecimal(rr.getGNum());
						BigDecimal gNum2 = new BigDecimal(rr.getGNum2());
						if(num.compareTo(gNum) != -1 && num.compareTo(gNum2) != 1) {
							shengcheng(num,toolWave,toolExpressSumTaskParam,rr,r,listsave,falg,v1,user);
							if (falg) {
								break;
							}
						}
					}
					if(rr.getGoodsMode().equals("大于")) {
						BigDecimal gNum = new BigDecimal(rr.getGNum());
						if(num.compareTo(gNum) == 1) {
							shengcheng(num,toolWave,toolExpressSumTaskParam,rr,r,listsave,falg,v1,user);
							if (falg) {
								break;
							}
						}
					}
					if(rr.getGoodsMode().equals("小于")) {
						BigDecimal gNum = new BigDecimal(rr.getGNum());
						if(num.compareTo(gNum) == -1) {
							shengcheng(num,toolWave,toolExpressSumTaskParam,rr,r,listsave,falg,v1,user);
							if (falg) {
								break;
							}
						}
					}
					if(rr.getGoodsMode().equals("等于")) {
						BigDecimal gNum = new BigDecimal(rr.getGNum());
						if(num.compareTo(gNum) == 0) {
							shengcheng(num,toolWave,toolExpressSumTaskParam,rr,r,listsave,falg,v1,user);
							if (falg) {
								break;
							}
						}
					}
					if(rr.getGoodsMode().equals("大于等于")) {
						BigDecimal gNum = new BigDecimal(rr.getGNum());
						if(num.compareTo(gNum) == 0 || num.compareTo(gNum) == 1) {
							shengcheng(num,toolWave,toolExpressSumTaskParam,rr,r,listsave,falg,v1,user);
							if (falg) {
								break;
							}
						}
					}
					if(rr.getGoodsMode().equals("小于等于")) {
						BigDecimal gNum = new BigDecimal(rr.getGNum());
						if(num.compareTo(gNum) == 0 || num.compareTo(gNum) == -1) {
							shengcheng(num,toolWave,toolExpressSumTaskParam,rr,r,listsave,falg,v1,user);
							if (falg) {
								break;
							}
						}
					}
				}
			}
			if (falg) {
				break;
			}
		}
    	if(listsave.size()>0) {
    		toolTaskMoneyService.saveBatchList(listsave);
    	}
		return ResponseData.success();
	}

	private void shengcheng(BigDecimal num, ToolWave toolWave, ToolExpressSumTaskParam toolExpressSumTaskParam,
			ToolInstallResult rr, ToolExpressSumTaskResult r, List<ToolTaskMoneyParam> listsave, boolean falg,
			BigDecimal v1,User user) {
		if(rr.getOrderMode().equals("区间")) {
			BigDecimal oNum = new BigDecimal(rr.getONum());
			BigDecimal oNum2 = new BigDecimal(rr.getONum2());
			if(v1.compareTo(oNum) != -1 && v1.compareTo(oNum2) != 1) {
				BigDecimal price = new BigDecimal(rr.getPrice());
				if(ToolUtil.isNotEmpty(r.getV3())) {
					if(r.getV3().equals("直贴")) {
						price = new BigDecimal(0.03).setScale(2, BigDecimal.ROUND_HALF_DOWN);
					}
					if(r.getV3().equals("预包")) {
						BigDecimal sub = new BigDecimal(0.03).setScale(2, BigDecimal.ROUND_HALF_DOWN);
						price = price.subtract(sub).setScale(2, BigDecimal.ROUND_HALF_DOWN);
					}
					if(r.getV3().equals("退货整理")) {
						price = new BigDecimal(0.2).setScale(2, BigDecimal.ROUND_HALF_DOWN);
					}
				}
        		String[] strs = r.getUserName().split("，");
        		if(strs.length > 1) {
    				BigDecimal users = new BigDecimal((strs.length));
    				BigDecimal orderNumd = v1.divide(users,2,BigDecimal.ROUND_HALF_DOWN);
    				BigDecimal money = orderNumd.multiply(price).setScale(2, BigDecimal.ROUND_HALF_DOWN);
        			for (String str : strs) {
            			ToolTaskMoneyParam taskMoneyParam = new ToolTaskMoneyParam();
            			taskMoneyParam.setV2(r.getV2());//波次
            			taskMoneyParam.setV1(toolWave.getWaveName());//作业日期
            			taskMoneyParam.setWaveId(toolExpressSumTaskParam.getWaveId());
            			taskMoneyParam.setCustomerId(r.getCustomerId());
            			taskMoneyParam.setCustomerName(r.getCustomerName());
            			taskMoneyParam.setArrangeId(r.getArrangeId());
            			taskMoneyParam.setArrangeNumber(r.getArrangeNumber());
            			taskMoneyParam.setUserName(str);
            			taskMoneyParam.setGoodsNumber(r.getGoodsNumber());//商品组合
            			taskMoneyParam.setNum(r.getNum()+"");//组合数量
            			taskMoneyParam.setOrderNum(orderNumd.toString());//单量
            			taskMoneyParam.setMoney(money.toString());//金额
            			taskMoneyParam.setDeptId(user.getDeptId());
            			taskMoneyParam.setV3(r.getV3());//类型
            			taskMoneyParam.setV4(orderNumd.multiply(num).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());//作业数
            			taskMoneyParam.setSkuNum(r.getGoodsNumber().split("，").length+"");//sku数
            			listsave.add(taskMoneyParam);
    				}
        			falg = true;
        			return;
        		} else {
        			ToolTaskMoneyParam taskMoneyParam = new ToolTaskMoneyParam();
        			taskMoneyParam.setV2(r.getV2());
        			taskMoneyParam.setV1(toolWave.getWaveName());
        			taskMoneyParam.setWaveId(toolExpressSumTaskParam.getWaveId());
        			taskMoneyParam.setCustomerId(r.getCustomerId());
        			taskMoneyParam.setCustomerName(r.getCustomerName());
        			taskMoneyParam.setArrangeId(r.getArrangeId());
        			taskMoneyParam.setArrangeNumber(r.getArrangeNumber());
        			taskMoneyParam.setUserName(r.getUserName());
        			taskMoneyParam.setGoodsNumber(r.getGoodsNumber());
        			taskMoneyParam.setNum(r.getNum()+"");
        			taskMoneyParam.setOrderNum(v1.toString());
        			taskMoneyParam.setMoney(v1.multiply(price).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
        			taskMoneyParam.setDeptId(user.getDeptId());
        			taskMoneyParam.setV3(r.getV3());
        			taskMoneyParam.setV4(v1.multiply(num).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
        			taskMoneyParam.setSkuNum(r.getGoodsNumber().split("，").length+"");
        			listsave.add(taskMoneyParam);
        			falg = true;
        			return;
        		}
			}
		} 
		if(rr.getOrderMode().equals("大于")) {
			BigDecimal oNum = new BigDecimal(rr.getONum());
			if(v1.compareTo(oNum) == 1) {
				BigDecimal price = new BigDecimal(rr.getPrice());
				if(ToolUtil.isNotEmpty(r.getV3())) {
					if(r.getV3().equals("直贴")) {
						price = new BigDecimal(0.03).setScale(2, BigDecimal.ROUND_HALF_DOWN);
					}
					if(r.getV3().equals("预包")) {
						BigDecimal sub = new BigDecimal(0.03).setScale(2, BigDecimal.ROUND_HALF_DOWN);
						price = price.subtract(sub).setScale(2, BigDecimal.ROUND_HALF_DOWN);
					}
					if(r.getV3().equals("退货整理")) {
						price = new BigDecimal(0.2).setScale(2, BigDecimal.ROUND_HALF_DOWN);
					}
				}
        		String[] strs = r.getUserName().split("，");
        		if(strs.length > 1) {
    				BigDecimal users = new BigDecimal((strs.length));
    				BigDecimal orderNumd = v1.divide(users,2,BigDecimal.ROUND_HALF_DOWN);
    				BigDecimal money = orderNumd.multiply(price).setScale(2, BigDecimal.ROUND_HALF_DOWN);
        			for (String str : strs) {
            			ToolTaskMoneyParam taskMoneyParam = new ToolTaskMoneyParam();
            			taskMoneyParam.setV2(r.getV2());
            			taskMoneyParam.setV1(toolWave.getWaveName());
            			taskMoneyParam.setWaveId(toolExpressSumTaskParam.getWaveId());
            			taskMoneyParam.setCustomerId(r.getCustomerId());
            			taskMoneyParam.setCustomerName(r.getCustomerName());
            			taskMoneyParam.setArrangeId(r.getArrangeId());
            			taskMoneyParam.setArrangeNumber(r.getArrangeNumber());
            			taskMoneyParam.setUserName(str);
            			taskMoneyParam.setGoodsNumber(r.getGoodsNumber());
            			taskMoneyParam.setNum(r.getNum()+"");
            			taskMoneyParam.setOrderNum(orderNumd.toString());
            			taskMoneyParam.setMoney(money.toString());
            			taskMoneyParam.setDeptId(user.getDeptId());
            			taskMoneyParam.setV3(r.getV3());
            			taskMoneyParam.setV4(orderNumd.multiply(num).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
            			taskMoneyParam.setSkuNum(r.getGoodsNumber().split("，").length+"");
            			listsave.add(taskMoneyParam);
    				}
        			falg = true;
        			return;
        		} else {
        			ToolTaskMoneyParam taskMoneyParam = new ToolTaskMoneyParam();
        			taskMoneyParam.setV2(r.getV2());
        			taskMoneyParam.setV1(toolWave.getWaveName());
        			taskMoneyParam.setWaveId(toolExpressSumTaskParam.getWaveId());
        			taskMoneyParam.setCustomerId(r.getCustomerId());
        			taskMoneyParam.setCustomerName(r.getCustomerName());
        			taskMoneyParam.setArrangeId(r.getArrangeId());
        			taskMoneyParam.setArrangeNumber(r.getArrangeNumber());
        			taskMoneyParam.setUserName(r.getUserName());
        			taskMoneyParam.setGoodsNumber(r.getGoodsNumber());
        			taskMoneyParam.setNum(r.getNum()+"");
        			taskMoneyParam.setOrderNum(v1.toString());
        			taskMoneyParam.setMoney(v1.multiply(price).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
        			taskMoneyParam.setDeptId(user.getDeptId());
        			taskMoneyParam.setV3(r.getV3());
        			taskMoneyParam.setV4(v1.multiply(num).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
        			taskMoneyParam.setSkuNum(r.getGoodsNumber().split("，").length+"");
        			listsave.add(taskMoneyParam);
        			falg = true;
        			return;
        		}
			}
		}
		if(rr.getOrderMode().equals("小于")) {
			BigDecimal oNum = new BigDecimal(rr.getONum());
			if(v1.compareTo(oNum) == -1) {
				BigDecimal price = new BigDecimal(rr.getPrice());
				if(ToolUtil.isNotEmpty(r.getV3())) {
					if(r.getV3().equals("直贴")) {
						price = new BigDecimal(0.03).setScale(2, BigDecimal.ROUND_HALF_DOWN);
					}
					if(r.getV3().equals("预包")) {
						BigDecimal sub = new BigDecimal(0.03).setScale(2, BigDecimal.ROUND_HALF_DOWN);
						price = price.subtract(sub).setScale(2, BigDecimal.ROUND_HALF_DOWN);
					}
					if(r.getV3().equals("退货整理")) {
						price = new BigDecimal(0.2).setScale(2, BigDecimal.ROUND_HALF_DOWN);
					}
				}
        		String[] strs = r.getUserName().split("，");
        		if(strs.length > 1) {
    				BigDecimal users = new BigDecimal((strs.length));
    				BigDecimal orderNumd = v1.divide(users,2,BigDecimal.ROUND_HALF_DOWN);
    				BigDecimal money = orderNumd.multiply(price).setScale(2, BigDecimal.ROUND_HALF_DOWN);
        			for (String str : strs) {
            			ToolTaskMoneyParam taskMoneyParam = new ToolTaskMoneyParam();
            			taskMoneyParam.setV2(r.getV2());
            			taskMoneyParam.setV1(toolWave.getWaveName());
            			taskMoneyParam.setWaveId(toolExpressSumTaskParam.getWaveId());
            			taskMoneyParam.setCustomerId(r.getCustomerId());
            			taskMoneyParam.setCustomerName(r.getCustomerName());
            			taskMoneyParam.setArrangeId(r.getArrangeId());
            			taskMoneyParam.setArrangeNumber(r.getArrangeNumber());
            			taskMoneyParam.setUserName(str);
            			taskMoneyParam.setGoodsNumber(r.getGoodsNumber());
            			taskMoneyParam.setNum(r.getNum()+"");
            			taskMoneyParam.setOrderNum(orderNumd.toString());
            			taskMoneyParam.setMoney(money.toString());
            			taskMoneyParam.setDeptId(user.getDeptId());
            			taskMoneyParam.setV3(r.getV3());
            			taskMoneyParam.setV4(orderNumd.multiply(num).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
            			taskMoneyParam.setSkuNum(r.getGoodsNumber().split("，").length+"");
            			listsave.add(taskMoneyParam);
    				}
        			falg = true;
        			return;
        		} else {
        			ToolTaskMoneyParam taskMoneyParam = new ToolTaskMoneyParam();
        			taskMoneyParam.setV2(r.getV2());
        			taskMoneyParam.setV1(toolWave.getWaveName());
        			taskMoneyParam.setWaveId(toolExpressSumTaskParam.getWaveId());
        			taskMoneyParam.setCustomerId(r.getCustomerId());
        			taskMoneyParam.setCustomerName(r.getCustomerName());
        			taskMoneyParam.setArrangeId(r.getArrangeId());
        			taskMoneyParam.setArrangeNumber(r.getArrangeNumber());
        			taskMoneyParam.setUserName(r.getUserName());
        			taskMoneyParam.setGoodsNumber(r.getGoodsNumber());
        			taskMoneyParam.setNum(r.getNum()+"");
        			taskMoneyParam.setOrderNum(v1.toString());
        			taskMoneyParam.setMoney(v1.multiply(price).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
        			taskMoneyParam.setDeptId(user.getDeptId());
        			taskMoneyParam.setV3(r.getV3());
        			taskMoneyParam.setV4(v1.multiply(num).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
        			taskMoneyParam.setSkuNum(r.getGoodsNumber().split("，").length+"");
        			listsave.add(taskMoneyParam);
        			falg = true;
        			return;
        		}
			}
		}
		if(rr.getOrderMode().equals("等于")) {
			BigDecimal oNum = new BigDecimal(rr.getONum());
			if(v1.compareTo(oNum) == 0) {
				BigDecimal price = new BigDecimal(rr.getPrice());
				if(ToolUtil.isNotEmpty(r.getV3())) {
					if(r.getV3().equals("直贴")) {
						price = new BigDecimal(0.03).setScale(2, BigDecimal.ROUND_HALF_DOWN);
					}
					if(r.getV3().equals("预包")) {
						BigDecimal sub = new BigDecimal(0.03).setScale(2, BigDecimal.ROUND_HALF_DOWN);
						price = price.subtract(sub).setScale(2, BigDecimal.ROUND_HALF_DOWN);
					}
					if(r.getV3().equals("退货整理")) {
						price = new BigDecimal(0.2).setScale(2, BigDecimal.ROUND_HALF_DOWN);
					}
				}
        		String[] strs = r.getUserName().split("，");
        		if(strs.length > 1) {
    				BigDecimal users = new BigDecimal((strs.length));
    				BigDecimal orderNumd = v1.divide(users,2,BigDecimal.ROUND_HALF_DOWN);
    				BigDecimal money = orderNumd.multiply(price).setScale(2, BigDecimal.ROUND_HALF_DOWN);
        			for (String str : strs) {
            			ToolTaskMoneyParam taskMoneyParam = new ToolTaskMoneyParam();
            			taskMoneyParam.setV2(r.getV2());
            			taskMoneyParam.setV1(toolWave.getWaveName());
            			taskMoneyParam.setWaveId(toolExpressSumTaskParam.getWaveId());
            			taskMoneyParam.setCustomerId(r.getCustomerId());
            			taskMoneyParam.setCustomerName(r.getCustomerName());
            			taskMoneyParam.setArrangeId(r.getArrangeId());
            			taskMoneyParam.setArrangeNumber(r.getArrangeNumber());
            			taskMoneyParam.setUserName(str);
            			taskMoneyParam.setGoodsNumber(r.getGoodsNumber());
            			taskMoneyParam.setNum(r.getNum()+"");
            			taskMoneyParam.setOrderNum(orderNumd.toString());
            			taskMoneyParam.setMoney(money.toString());
            			taskMoneyParam.setDeptId(user.getDeptId());
            			taskMoneyParam.setV3(r.getV3());
            			taskMoneyParam.setV4(orderNumd.multiply(num).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
            			taskMoneyParam.setSkuNum(r.getGoodsNumber().split("，").length+"");
            			listsave.add(taskMoneyParam);
    				}
        			falg = true;
        			return;
        		} else {
        			ToolTaskMoneyParam taskMoneyParam = new ToolTaskMoneyParam();
        			taskMoneyParam.setV2(r.getV2());
        			taskMoneyParam.setV1(toolWave.getWaveName());
        			taskMoneyParam.setWaveId(toolExpressSumTaskParam.getWaveId());
        			taskMoneyParam.setCustomerId(r.getCustomerId());
        			taskMoneyParam.setCustomerName(r.getCustomerName());
        			taskMoneyParam.setArrangeId(r.getArrangeId());
        			taskMoneyParam.setArrangeNumber(r.getArrangeNumber());
        			taskMoneyParam.setUserName(r.getUserName());
        			taskMoneyParam.setGoodsNumber(r.getGoodsNumber());
        			taskMoneyParam.setNum(r.getNum()+"");
        			taskMoneyParam.setOrderNum(v1.toString());
        			taskMoneyParam.setMoney(v1.multiply(price).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
        			taskMoneyParam.setDeptId(user.getDeptId());
        			taskMoneyParam.setV3(r.getV3());
        			taskMoneyParam.setV4(v1.multiply(num).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
        			taskMoneyParam.setSkuNum(r.getGoodsNumber().split("，").length+"");
        			listsave.add(taskMoneyParam);
        			falg = true;
        			return;
        		}
			}
		}
		if(rr.getOrderMode().equals("大于等于")) {
			BigDecimal oNum = new BigDecimal(rr.getONum());
			if(v1.compareTo(oNum) == 1 || v1.compareTo(oNum) == 0) {
				BigDecimal price = new BigDecimal(rr.getPrice());
				if(ToolUtil.isNotEmpty(r.getV3())) {
					if(r.getV3().equals("直贴")) {
						price = new BigDecimal(0.03).setScale(2, BigDecimal.ROUND_HALF_DOWN);
					}
					if(r.getV3().equals("预包")) {
						BigDecimal sub = new BigDecimal(0.03).setScale(2, BigDecimal.ROUND_HALF_DOWN);
						price = price.subtract(sub).setScale(2, BigDecimal.ROUND_HALF_DOWN);
					}
					if(r.getV3().equals("退货整理")) {
						price = new BigDecimal(0.2).setScale(2, BigDecimal.ROUND_HALF_DOWN);
					}
				}
        		String[] strs = r.getUserName().split("，");
        		if(strs.length > 1) {
    				BigDecimal users = new BigDecimal((strs.length));
    				BigDecimal orderNumd = v1.divide(users,2,BigDecimal.ROUND_HALF_DOWN);
    				BigDecimal money = orderNumd.multiply(price).setScale(2, BigDecimal.ROUND_HALF_DOWN);
        			for (String str : strs) {
            			ToolTaskMoneyParam taskMoneyParam = new ToolTaskMoneyParam();
            			taskMoneyParam.setV2(r.getV2());
            			taskMoneyParam.setV1(toolWave.getWaveName());
            			taskMoneyParam.setWaveId(toolExpressSumTaskParam.getWaveId());
            			taskMoneyParam.setCustomerId(r.getCustomerId());
            			taskMoneyParam.setCustomerName(r.getCustomerName());
            			taskMoneyParam.setArrangeId(r.getArrangeId());
            			taskMoneyParam.setArrangeNumber(r.getArrangeNumber());
            			taskMoneyParam.setUserName(str);
            			taskMoneyParam.setGoodsNumber(r.getGoodsNumber());
            			taskMoneyParam.setNum(r.getNum()+"");
            			taskMoneyParam.setOrderNum(orderNumd.toString());
            			taskMoneyParam.setMoney(money.toString());
            			taskMoneyParam.setDeptId(user.getDeptId());
            			taskMoneyParam.setV3(r.getV3());
            			taskMoneyParam.setV4(orderNumd.multiply(num).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
            			taskMoneyParam.setSkuNum(r.getGoodsNumber().split("，").length+"");
            			listsave.add(taskMoneyParam);
    				}
        			falg = true;
        			return;
        		} else {
        			ToolTaskMoneyParam taskMoneyParam = new ToolTaskMoneyParam();
        			taskMoneyParam.setV2(r.getV2());
        			taskMoneyParam.setV1(toolWave.getWaveName());
        			taskMoneyParam.setWaveId(toolExpressSumTaskParam.getWaveId());
        			taskMoneyParam.setCustomerId(r.getCustomerId());
        			taskMoneyParam.setCustomerName(r.getCustomerName());
        			taskMoneyParam.setArrangeId(r.getArrangeId());
        			taskMoneyParam.setArrangeNumber(r.getArrangeNumber());
        			taskMoneyParam.setUserName(r.getUserName());
        			taskMoneyParam.setGoodsNumber(r.getGoodsNumber());
        			taskMoneyParam.setNum(r.getNum()+"");
        			taskMoneyParam.setOrderNum(v1.toString());
        			taskMoneyParam.setMoney(v1.multiply(price).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
        			taskMoneyParam.setDeptId(user.getDeptId());
        			taskMoneyParam.setV3(r.getV3());
        			taskMoneyParam.setV4(v1.multiply(num).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
        			taskMoneyParam.setSkuNum(r.getGoodsNumber().split("，").length+"");
        			listsave.add(taskMoneyParam);
        			falg = true;
        			return;
        		}
			}
		}
		if(rr.getOrderMode().equals("小于等于")) {
			BigDecimal oNum = new BigDecimal(rr.getONum());
			if(v1.compareTo(oNum) == -1 || v1.compareTo(oNum) == 0) {
				BigDecimal price = new BigDecimal(rr.getPrice());
				if(ToolUtil.isNotEmpty(r.getV3())) {
					if(r.getV3().equals("直贴")) {
						price = new BigDecimal(0.03).setScale(2, BigDecimal.ROUND_HALF_DOWN);
					}
					if(r.getV3().equals("预包")) {
						BigDecimal sub = new BigDecimal(0.03).setScale(2, BigDecimal.ROUND_HALF_DOWN);
						price = price.subtract(sub).setScale(2, BigDecimal.ROUND_HALF_DOWN);
					}
					if(r.getV3().equals("退货整理")) {
						price = new BigDecimal(0.2).setScale(2, BigDecimal.ROUND_HALF_DOWN);
					}
				}
        		String[] strs = r.getUserName().split("，");
        		if(strs.length > 1) {
    				BigDecimal users = new BigDecimal((strs.length));
    				BigDecimal orderNumd = v1.divide(users,2,BigDecimal.ROUND_HALF_DOWN);
    				BigDecimal money = orderNumd.multiply(price).setScale(2, BigDecimal.ROUND_HALF_DOWN);
        			for (String str : strs) {
            			ToolTaskMoneyParam taskMoneyParam = new ToolTaskMoneyParam();
            			taskMoneyParam.setV2(r.getV2());
            			taskMoneyParam.setV1(toolWave.getWaveName());
            			taskMoneyParam.setWaveId(toolExpressSumTaskParam.getWaveId());
            			taskMoneyParam.setCustomerId(r.getCustomerId());
            			taskMoneyParam.setCustomerName(r.getCustomerName());
            			taskMoneyParam.setArrangeId(r.getArrangeId());
            			taskMoneyParam.setArrangeNumber(r.getArrangeNumber());
            			taskMoneyParam.setUserName(str);
            			taskMoneyParam.setGoodsNumber(r.getGoodsNumber());
            			taskMoneyParam.setNum(r.getNum()+"");
            			taskMoneyParam.setOrderNum(orderNumd.toString());
            			taskMoneyParam.setMoney(money.toString());
            			taskMoneyParam.setDeptId(user.getDeptId());
            			taskMoneyParam.setV3(r.getV3());
            			taskMoneyParam.setV4(orderNumd.multiply(num).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
            			taskMoneyParam.setSkuNum(r.getGoodsNumber().split("，").length+"");
            			listsave.add(taskMoneyParam);
    				}
        			falg = true;
        			return;
        		} else {
        			ToolTaskMoneyParam taskMoneyParam = new ToolTaskMoneyParam();
        			taskMoneyParam.setV2(r.getV2());
        			taskMoneyParam.setV1(toolWave.getWaveName());
        			taskMoneyParam.setWaveId(toolExpressSumTaskParam.getWaveId());
        			taskMoneyParam.setCustomerId(r.getCustomerId());
        			taskMoneyParam.setCustomerName(r.getCustomerName());
        			taskMoneyParam.setArrangeId(r.getArrangeId());
        			taskMoneyParam.setArrangeNumber(r.getArrangeNumber());
        			taskMoneyParam.setUserName(r.getUserName());
        			taskMoneyParam.setGoodsNumber(r.getGoodsNumber());
        			taskMoneyParam.setNum(r.getNum()+"");
        			taskMoneyParam.setOrderNum(v1.toString());
        			taskMoneyParam.setMoney(v1.multiply(price).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
        			taskMoneyParam.setDeptId(user.getDeptId());
        			taskMoneyParam.setV3(r.getV3());
        			taskMoneyParam.setV4(v1.multiply(num).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
        			taskMoneyParam.setSkuNum(r.getGoodsNumber().split("，").length+"");
        			listsave.add(taskMoneyParam);
        			falg = true;
        			return;
        		}
			}
		}
	}

}
